package handle

import (
	"bytes"
	"fmt"
	"strings"
	"time"
)

var (
	CheckInOut_Tpl         = "*%s*\n  [%s]\n{:line}{:item}"
	CheckInOut_Stat_Line   = "-----------------------------\n"
	CheckIn_Stat_Item      = "*%s*\n[出勤]:%s\n[缺席]:%s\n[健康]:%s\n[服药]:%s\n[观察]:%s\n"
	CheckIn_Data_Item_Name = "*%s*:\n"
	CheckIn_Data_Item      = "[%s] %s\n"
	CheckOut_Stat_Item     = "*%s*\n[已接走]:%s\n[应接走]:%s\n[未接走]:%s\n[接走率]:%s\n"
	Option_Tpl             = "\n[%d] %s"
)

type StatList interface {
	Add(grade string, groupId int, statItem interface{})
}

type CheckInStatList []struct {
	string
	int
	*CheckInStat
}

type CheckOutStatList []struct {
	string
	int
	*CheckOutStat
}

type Options []struct {
	string
	int
}

type CheckInOutList struct {
	Name string
	Data []CheckInOutData
}

func NewCheckInStatList() CheckInStatList {
	return make([]struct {
		string
		int
		*CheckInStat
	}, 0)
}

func (list *CheckInStatList) Add(grade string, groupId int, statItem interface{}) {
	if grade != "" {
		*list = append(*list, struct {
			string
			int
			*CheckInStat
		}{grade, groupId, statItem.(*CheckInStat)})
	}
}

func (list *CheckInStatList) Output(title string) string {
	var itemBuf bytes.Buffer
	tpl := fmt.Sprintf(CheckInOut_Tpl, title, time.Now().Format("2006-01-02"))
	for _, v := range *list {
		itemBuf.WriteString(fmt.Sprintf(CheckIn_Stat_Item, v.string, v.In, v.Abs, v.Health, v.Sick, v.Watch))
		itemBuf.WriteString(CheckInOut_Stat_Line)
	}
	tpl = strings.Replace(tpl, "{:line}", CheckInOut_Stat_Line, -1)
	tpl = strings.Replace(tpl, "{:item}", itemBuf.String(), -1)
	return tpl
}

func NewCheckOutStatList() CheckOutStatList {
	return make([]struct {
		string
		int
		*CheckOutStat
	}, 0)
}

func (list *CheckOutStatList) Add(grade string, groupId int, statItem interface{}) {
	if grade != "" {
		*list = append(*list, struct {
			string
			int
			*CheckOutStat
		}{grade, groupId, statItem.(*CheckOutStat)})
	}
}

func (list *CheckOutStatList) Output(title string) string {
	var itemBuf bytes.Buffer
	tpl := fmt.Sprintf(CheckInOut_Tpl, title, time.Now().Format("2006-01-02"))
	for _, v := range *list {
		itemBuf.WriteString(fmt.Sprintf(CheckOut_Stat_Item, v.string, v.Out, v.In, v.Stay, v.OutPer))
		itemBuf.WriteString(CheckInOut_Stat_Line)
	}
	tpl = strings.Replace(tpl, "{:line}", CheckInOut_Stat_Line, -1)
	tpl = strings.Replace(tpl, "{:item}", itemBuf.String(), -1)
	return tpl
}

func NewOptions() Options {
	return make([]struct {
		string
		int
	}, 0)
}

func (opts *Options) Add(val string, groupId int) {
	if val != "" {
		*opts = append(*opts, struct {
			string
			int
		}{val, groupId})
	}
}

func (opts Options) Output(title string) string {
	var optBuf bytes.Buffer
	for _, opt := range opts {
		optBuf.WriteString(fmt.Sprintf(Option_Tpl, opt.int, opt.string))
	}
	if len(opts) != 0 {
		title = "*请回复序号选择*:"
	}
	return fmt.Sprintf("%s%s", title, optBuf.String())
}

func NewCheckInOutList(name string) CheckInOutList {
	return CheckInOutList{Name: name, Data: make([]CheckInOutData, 0)}
}

func (list *CheckInOutList) AddData(state, time string) {
	if state != "" {
		list.Data = append(list.Data, CheckInOutData{State: state, Time: time})
	}
}

func (list CheckInOutList) Output(title string) string {
	var itemBuf bytes.Buffer
	tpl := fmt.Sprintf(CheckInOut_Tpl, title, time.Now().Format("2006-01-02"))
	itemBuf.WriteString(fmt.Sprintf(CheckIn_Data_Item_Name, list.Name))
	for _, v := range list.Data {
		itemBuf.WriteString(fmt.Sprintf(CheckIn_Data_Item, v.State, v.Time))
	}
	tpl = strings.Replace(tpl, "{:line}", CheckInOut_Stat_Line, -1)
	tpl = strings.Replace(tpl, "{:item}", itemBuf.String(), -1)
	return tpl
}
